home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 17 / CU Amiga Magazine's Super CD-ROM 17 (1997)(EMAP Images)(GB)[!][issue 1997-12].iso / CUCD / Programming / DiceSource / lib / amiga / c_pi.a < prev    next >
Text File  |  1994-02-01  |  4KB  |  177 lines

  1.  
  2.         ;   C_PI.A
  3.         ;
  4.         ;   (c)Copyright 1990, Matthew Dillon, All Rights Reserved
  5.         ;
  6.         ;   Amiga startup code for non-resident DCC programs where
  7.         ;   the entire program is in one CODE hunk.  The CODE hunk
  8.         ;   is pre-extended for the BSS space so we need only clear
  9.         ;   it.
  10.         ;
  11.         ;   DLINK:
  12.         ;
  13.         ;   __BSS_LEN    : # long wrds of bss (occurs after DATA)
  14.         ;   __DATA_LEN    : # long wrds of initialized data to copy (if resident)
  15.         ;   __DATA_BAS    : read-write, PC-REL initiali access to set up A4
  16.  
  17.         section text,code
  18.  
  19.         xref    __BSS_LEN        ; (dlink), length of BSS
  20.         xref    __DATA_BAS        ; (dlink), base of initialized data
  21.         xref    __DATA_LEN        ; (dlink), length of data
  22.  
  23.         xref    __main            ; we call _main()
  24.  
  25.         xref    _LVOSetSignal
  26.         xref    _LVOAllocMem
  27.         xref    _LVOFreeMem
  28.         xref    _LVOForbid
  29.         xref    _LVOReplyMsg
  30.  
  31.         xdef    __exit            ; we are _exit()
  32.         xdef    start
  33.         xdef    _SysBase        ; we export _SysBase
  34.         xdef    __WBMsg
  35.  
  36. start:
  37. ;;;;;;;;;;;;;;;;movem.l D2-D7/A2-A6,-(sp)       ;Not needed!
  38.  
  39.         move.l    4.W,A6            ; EXEC base
  40.  
  41.         move.l    sp,A2
  42.         move.l    A0,-(sp)            ; save arg for _main() call
  43.         move.l    D0,-(sp)            ; save arglen for _main() call
  44.  
  45.         lea    __DATA_BAS(pc),A4
  46.         lea    32766(A4),A4
  47.         sub.l    A3,A3
  48.  
  49. clrbss
  50.         ;   CLEAR BSS    &-32766(A4) + __DATA_LEN*4
  51.  
  52.         lea    -32766(A4),A0
  53.         move.l    #__DATA_LEN,D0
  54.         asl.l    #2,D0
  55.         add.l    D0,A0
  56.  
  57.         move.l    #__BSS_LEN,D0        ; longwords of bss
  58.         moveq.l #0,D1
  59.         bra    clrent
  60. clrlop        move.l    D1,(A0)+
  61. clrent        dbf    D0,clrlop
  62.         sub.l    #$10000,D0
  63.         bcc    clrlop
  64.  
  65.         move.l    A3,___MemList(A4)   ; memlist entry (if resident)
  66.         move.l    A2,__ExitSP(A4)     ; sp to restore
  67.  
  68.         moveq.l #0,D0            ; new signals
  69.         move.l    #$1000,D1        ; signal mask
  70.         jsr    _LVOSetSignal(A6)   ; clear ^C
  71.  
  72.         move.l    A6,_SysBase(A4)     ; resident segment.
  73.  
  74.         ;   Return value of 0 indicates succcess.  Flags
  75.         ;   have been set.
  76.  
  77.         jsr    __AutoInit0(pc)     ; A6 has SYSBase
  78.         bne    xfail
  79.         pea    1.W            ; autoconfig loader
  80.         jsr    __AutoConfig(pc)    ; note, rt pulls stack
  81.         addq.l    #4,sp
  82.         jsr    __AutoInit1(pc)     ; A6 has SYSBase
  83.         bne    xfail
  84.         jsr    __main(pc)
  85.  
  86.         ;   fall through to low level exit... this avoids referencing
  87.         ;   exit() if the user overides _main().
  88.  
  89. xfail
  90.         clr.l    -(sp)
  91.         clr.l    -(sp)
  92.  
  93.         ;   _EXIT()
  94.         ;
  95.         ;   since entry uses malloc we must free any incidental memory
  96.         ;   at __exit instead of _exit.
  97.         ;
  98.         ;   ReplyMsg(_WBMsg) just before returning
  99.  
  100. __exit:
  101.         pea    -1.W
  102.         jsr    __AutoConfig(pc)    ; note, rt pulls stack
  103.         addq.l    #4,sp
  104.         move.l    _SysBase(A4),A6
  105.         jsr    __AutoExit1(pc)     ; A6 has SysBase
  106.         jsr    __AutoExit0(pc)     ; A6 has SysBase
  107.  
  108.         move.l    __ExitSP(A4),A5     ; get sp... because we might free
  109.                         ; the space taken by the variable!
  110.  
  111.         move.l    __WBMsg(A4),D6      ; D6 = WBMsg if it exists
  112.  
  113.         move.l    ___MemList(A4),D0   ; free memory
  114.         beq    ex20
  115. ex10        move.l    D0,A2
  116.         move.l    (A2),A3             ; next...
  117.  
  118.         move.l    4(A2),D0            ; bytes
  119.         move.l    A2,A1            ; ptr
  120.         jsr    _LVOFreeMem(A6)
  121.  
  122.         move.l    A3,D0            ; next...
  123.         bne    ex10
  124. ex20
  125.  
  126.         move.l    4(sp),D0            ; get exit code
  127.         move.l    A5,sp            ; restore sp
  128.  
  129.         tst.l    D6            ; reply to workbench msg if it
  130.         beq    ex30            ; exists
  131.         jsr    _LVOForbid(A6)      ; forbid through exit
  132.         move.l    D6,A1
  133.         jsr    _LVOReplyMsg(A6)
  134.  
  135. ex30
  136.         ;   FINIS, poof.
  137.  
  138. exfail
  139. ;;;;;;;;;;;;;;;;movem.l (sp)+,D2-D7/A2-A6
  140.         rts
  141.  
  142.  
  143.         ;   Base of autoinit section
  144.  
  145.         section autoinit0,code
  146. __AutoInit0:
  147.         section autoinit1,code
  148. __AutoInit1:
  149.         section autoexit0,code
  150. __AutoExit0:
  151.         section autoexit1,code
  152. __AutoExit1:
  153.         section autoconfig,code
  154. __AutoConfig:
  155.  
  156.         ;   All library C code is compiled with the -S option
  157.         ;   which uses 'libdata' and 'libbss' section names,
  158.         ;   forcing library data to come before program data
  159.         ;   and library bss to come before program bss (because
  160.         ;   library data/bss sections are declared here first
  161.         ;   and sections of like name are coagulated).
  162.  
  163.         section libdata,data
  164. _Reserved    dc.l    0        ; force section to exist (dummy)
  165.  
  166.         section libbss,bss
  167.  
  168.         xdef    ___MemList    ; used by malloc/free
  169.         xdef    __ExitSP
  170.  
  171. _SysBase    ds.l    1
  172. __ExitSP    ds.l    1
  173. __WBMsg     ds.l    1
  174. ___MemList    ds.l    1
  175.  
  176.         END
  177.